package com.hd.trans.files;

import android.view.Gravity;
import android.widget.Button;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hd.trans.R;
import com.hd.trans.files.adapter.SortFilesAdapter;
import com.hd.trans.files.interf.IFileSortListener;
import com.hd.trans.files.model.FileSortModel;
import com.hd.trans.files.sort.ISort;
import com.hd.trans.ui.base.TransInit;
import com.hd.trans.utils.SharedFileUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件排序列表
 */
public class FileSortDialogFragment extends BaseDialogFragment {

    private Button mDetermineBtn;

    private SortFilesAdapter mAdapter;
    private List<FileSortModel.ModeItem> mList = new ArrayList<>();
    private IFileSortListener mFileSortListener;
    private ISort.SortMethod mDefaultSortMethod;

    public void setFileSortListener(ISort.SortMethod sortMethod, IFileSortListener mFileSortListener) {
        this.mDefaultSortMethod = sortMethod;
        this.mFileSortListener = mFileSortListener;
    }

    public static FileSortDialogFragment newInstance() {
        return new FileSortDialogFragment();
    }

    @Override
    int getLayoutId() {
        return R.layout.dialog_sort_files_layout;
    }

    @Override
    void initView() {
        mDetermineBtn = mRootView.findViewById(R.id.sort_files_determine);
        RecyclerView sortRecyclerView = mRootView.findViewById(R.id.sort_files_list);
        mDetermineBtn = getView(R.id.sort_files_determine);
        mDetermineBtn.setOnClickListener(view -> dismiss());
        sortRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new SortFilesAdapter();
        sortRecyclerView.setAdapter(mAdapter);
        initData();
        initListener();

//        mViewModel = new ViewModelProvider(requireActivity()).get(PickingFileViewModel.class);
    }

    private void initData() {
        mList.clear();
        List<FileSortModel.ModeItem> modeItems = FileSortModel.getSortMode(requireContext());
        //获取上一次选择排序的方式
        if (mDefaultSortMethod != null) {
            for (int i = 0; i < modeItems.size(); i++) {
                if (modeItems.get(i).getMethod() == mDefaultSortMethod) {
                    modeItems.get(i).setSelect(true);
                } else {
                    modeItems.get(i).setSelect(false);
                }
            }
        }
        mList.addAll(modeItems);
        mAdapter.submitList(mList);
    }

    private void initListener() {
        mAdapter.setOnItemClickListener((viewId, modeItem, position) -> {
            boolean select = modeItem.isSelect();
            if (!select) {
                for (int i = 0; i < mList.size(); i++) {
                    if (i == position) {
                        mList.get(i).setSelect(true);
                        if (mFileSortListener != null) {
                            mFileSortListener.fileSort(mList.get(i));
                        }
                    } else {
                        mList.get(i).setSelect(false);
                    }
                }
            }
            mAdapter.notifyDataSetChanged();

            dismiss();
        });
        mDetermineBtn.setOnClickListener(v -> {
            List<FileSortModel.ModeItem> currentList = mAdapter.getCurrentList();
            for (int i = 0; i < currentList.size(); i++) {
                if (currentList.get(i).isSelect()) {
                    if (mFileSortListener != null) {
                        mFileSortListener.fileSort(currentList.get(i));
                    }
                    break;
                }
            }
            dismiss();
        });
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }
}
